home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / dos / mpegstat / decoders.c < prev    next >
C/C++ Source or Header  |  1997-01-01  |  34KB  |  961 lines

  1. /* MPEGSTAT - analyzing tool for MPEG-I video streams
  2.  * 
  3.  * Technical University of Berlin, Germany, Dept. of Computer Science
  4.  * Tom Pfeifer - Multimedia systems project - pfeifer@fokus.gmd.de
  5.  *
  6.  * Jens Brettin, Harald Masche, Alexander Schulze, Dirk Schubert
  7.  *
  8.  * This program uses parts of the source code of the Berkeley MPEG player
  9.  *
  10.  * ---------------------------
  11.  *
  12.  * Copyright (c) 1993 Technical University of Berlin, Germany
  13.  *
  14.  * for the parts of the Berkeley player used:
  15.  *
  16.  * Copyright (c) 1992 The Regents of the University of California.
  17.  * All rights reserved.
  18.  *
  19.  * ---------------------------
  20.  *
  21.  * Permission to use, copy, modify, and distribute this software and its
  22.  * documentation for any purpose, without fee, and without written agreement is
  23.  * hereby granted, provided that the above copyright notices and the following
  24.  * two paragraphs appear in all copies of this software.
  25.  * 
  26.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA 
  27.  * or the Technical University of Berlin BE LIABLE TO ANY PARTY FOR
  28.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  29.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  30.  * CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  31.  * POSSIBILITY OF SUCH DAMAGE.
  32.  * 
  33.  * THE UNIVERSITY OF CALIFORNIA and the Technical University of Berlin 
  34.  * SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  35.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  36.  * PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  37.  * UNIVERSITY OF CALIFORNIA and the Technical University of Berlin HAVE NO 
  38.  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  39.  * OR MODIFICATIONS.
  40.  */
  41. /*
  42.  * decoders.c
  43.  *
  44.  * This file contains all the routines for Huffman decoding required in 
  45.  * MPEG
  46.  *
  47.  */
  48.  
  49. #include <stdio.h>
  50. #include <assert.h>
  51. #include "decoders.h"
  52. #include "util.h" 
  53. #include "video.h"
  54. #include "proto.h"
  55. #include "dither.h"
  56.  
  57. /* Decoding table for macroblock_address_increment */
  58. mb_addr_inc_entry     mb_addr_inc[2048];
  59.  
  60. /* Decoding table for macroblock_type in predictive-coded pictures */
  61. mb_type_entry         mb_type_P[64];
  62.  
  63. /* Decoding table for macroblock_type in bidirectionally-coded pictures */
  64. mb_type_entry         mb_type_B[64];
  65.  
  66. /* Decoding table for motion vectors */
  67. motion_vectors_entry  motion_vectors[2048];
  68.  
  69. /* Decoding table for coded_block_pattern */
  70.  
  71. coded_block_pattern_entry coded_block_pattern[512] = 
  72. { {(unsigned int)ERROR, 0}, {(unsigned int)ERROR, 0}, {39, 9}, {27, 9}, {59, 9}, {55, 9}, {47, 9}, {31, 9},
  73.     {58, 8}, {58, 8}, {54, 8}, {54, 8}, {46, 8}, {46, 8}, {30, 8}, {30, 8},
  74.     {57, 8}, {57, 8}, {53, 8}, {53, 8}, {45, 8}, {45, 8}, {29, 8}, {29, 8},
  75.     {38, 8}, {38, 8}, {26, 8}, {26, 8}, {37, 8}, {37, 8}, {25, 8}, {25, 8},
  76.     {43, 8}, {43, 8}, {23, 8}, {23, 8}, {51, 8}, {51, 8}, {15, 8}, {15, 8},
  77.     {42, 8}, {42, 8}, {22, 8}, {22, 8}, {50, 8}, {50, 8}, {14, 8}, {14, 8},
  78.     {41, 8}, {41, 8}, {21, 8}, {21, 8}, {49, 8}, {49, 8}, {13, 8}, {13, 8},
  79.     {35, 8}, {35, 8}, {19, 8}, {19, 8}, {11, 8}, {11, 8}, {7, 8}, {7, 8},
  80.     {34, 7}, {34, 7}, {34, 7}, {34, 7}, {18, 7}, {18, 7}, {18, 7}, {18, 7},
  81.     {10, 7}, {10, 7}, {10, 7}, {10, 7}, {6, 7}, {6, 7}, {6, 7}, {6, 7}, 
  82.     {33, 7}, {33, 7}, {33, 7}, {33, 7}, {17, 7}, {17, 7}, {17, 7}, {17, 7}, 
  83.     {9, 7}, {9, 7}, {9, 7}, {9, 7}, {5, 7}, {5, 7}, {5, 7}, {5, 7}, 
  84.     {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, {63, 6}, 
  85.     {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, {3, 6}, 
  86.     {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, {36, 6}, 
  87.     {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, {24, 6}, 
  88.     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  89.     {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5}, {62, 5},
  90.     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  91.     {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, {2, 5}, 
  92.     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  93.     {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, {61, 5}, 
  94.     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  95.     {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, {1, 5}, 
  96.     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  97.     {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, {56, 5}, 
  98.     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  99.     {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, {52, 5}, 
  100.     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  101.     {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, {44, 5}, 
  102.     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  103.     {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, {28, 5}, 
  104.     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  105.     {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, {40, 5}, 
  106.     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  107.     {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, {20, 5}, 
  108.     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  109.     {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, {48, 5}, 
  110.     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  111.     {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, {12, 5}, 
  112.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  113.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  114.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  115.     {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, {32, 4}, 
  116.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  117.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  118.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  119.     {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, {16, 4}, 
  120.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  121.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  122.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, 
  123.     {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4}, {8, 4},
  124.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
  125.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  126.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  127.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4},
  128.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  129.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  130.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  131.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  132.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  133.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  134.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, 
  135.     {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}, {60, 3}
  136. };
  137.  
  138. /* Decoding table for dct_dc_size_luminance */
  139. dct_dc_size_entry dct_dc_size_luminance[128] =
  140. {   {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  141.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  142.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  143.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  144.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  145.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  146.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  147.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  148.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  149.     {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, {0, 3}, 
  150.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  151.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  152.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  153.     {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, {4, 3}, 
  154.     {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, {5, 4}, 
  155.     {6, 5}, {6, 5}, {6, 5}, {6, 5}, {7, 6}, {7, 6}, {8, 7}, {(unsigned int)ERROR, 0}
  156. };
  157.  
  158. /* Decoding table for dct_dc_size_chrominance */
  159. dct_dc_size_entry dct_dc_size_chrominance[256] =
  160. { {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  161.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  162.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  163.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  164.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  165.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  166.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  167.     {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, {0, 2}, 
  168.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  169.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  170.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  171.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  172.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  173.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  174.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  175.     {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, {1, 2}, 
  176.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  177.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  178.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  179.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  180.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  181.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  182.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  183.     {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, {2, 2}, 
  184.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  185.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  186.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  187.     {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, {3, 3}, 
  188.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  189.     {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, {4, 4}, 
  190.     {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, {5, 5}, 
  191.     {6, 6}, {6, 6}, {6, 6}, {6, 6}, {7, 7}, {7, 7}, {8, 8}, {(unsigned int)ERROR, 0}
  192. };
  193.  
  194. /* DCT coeff tables. */
  195.  
  196. unsigned short int dct_coeff_tbl_0[256] =
  197. {
  198. 0xffff, 0xffff, 0xffff, 0xffff, 
  199. 0xffff, 0xffff, 0xffff, 0xffff, 
  200. 0xffff, 0xffff, 0xffff, 0xffff, 
  201. 0xffff, 0xffff, 0xffff, 0xffff, 
  202. 0x052f, 0x051f, 0x050f, 0x04ff, 
  203. 0x183f, 0x402f, 0x3c2f, 0x382f, 
  204. 0x342f, 0x302f, 0x2c2f, 0x7c1f, 
  205. 0x781f, 0x741f, 0x701f, 0x6c1f, 
  206. 0x028e, 0x028e, 0x027e, 0x027e, 
  207. 0x026e, 0x026e, 0x025e, 0x025e, 
  208. 0x024e, 0x024e, 0x023e, 0x023e, 
  209. 0x022e, 0x022e, 0x021e, 0x021e, 
  210. 0x020e, 0x020e, 0x04ee, 0x04ee, 
  211. 0x04de, 0x04de, 0x04ce, 0x04ce, 
  212. 0x04be, 0x04be, 0x04ae, 0x04ae, 
  213. 0x049e, 0x049e, 0x048e, 0x048e, 
  214. 0x01fd, 0x01fd, 0x01fd, 0x01fd, 
  215. 0x01ed, 0x01ed, 0x01ed, 0x01ed, 
  216. 0x01dd, 0x01dd, 0x01dd, 0x01dd, 
  217. 0x01cd, 0x01cd, 0x01cd, 0x01cd, 
  218. 0x01bd, 0x01bd, 0x01bd, 0x01bd, 
  219. 0x01ad, 0x01ad, 0x01ad, 0x01ad, 
  220. 0x019d, 0x019d, 0x019d, 0x019d, 
  221. 0x018d, 0x018d, 0x018d, 0x018d, 
  222. 0x017d, 0x017d, 0x017d, 0x017d, 
  223. 0x016d, 0x016d, 0x016d, 0x016d, 
  224. 0x015d, 0x015d, 0x015d, 0x015d, 
  225. 0x014d, 0x014d, 0x014d, 0x014d, 
  226. 0x013d, 0x013d, 0x013d, 0x013d, 
  227. 0x012d, 0x012d, 0x012d, 0x012d, 
  228. 0x011d, 0x011d, 0x011d, 0x011d, 
  229. 0x010d, 0x010d, 0x010d, 0x010d, 
  230. 0x282c, 0x282c, 0x282c, 0x282c, 
  231. 0x282c, 0x282c, 0x282c, 0x282c, 
  232. 0x242c, 0x242c, 0x242c, 0x242c, 
  233. 0x242c, 0x242c, 0x242c, 0x242c, 
  234. 0x143c, 0x143c, 0x143c, 0x143c, 
  235. 0x143c, 0x143c, 0x143c, 0x143c, 
  236. 0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  237. 0x0c4c, 0x0c4c, 0x0c4c, 0x0c4c, 
  238. 0x085c, 0x085c, 0x085c, 0x085c, 
  239. 0x085c, 0x085c, 0x085c, 0x085c, 
  240. 0x047c, 0x047c, 0x047c, 0x047c, 
  241. 0x047c, 0x047c, 0x047c, 0x047c, 
  242. 0x046c, 0x046c, 0x046c, 0x046c, 
  243. 0x046c, 0x046c, 0x046c, 0x046c, 
  244. 0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  245. 0x00fc, 0x00fc, 0x00fc, 0x00fc, 
  246. 0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  247. 0x00ec, 0x00ec, 0x00ec, 0x00ec, 
  248. 0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  249. 0x00dc, 0x00dc, 0x00dc, 0x00dc, 
  250. 0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  251. 0x00cc, 0x00cc, 0x00cc, 0x00cc, 
  252. 0x681c, 0x681c, 0x681c, 0x681c, 
  253. 0x681c, 0x681c, 0x681c, 0x681c, 
  254. 0x641c, 0x641c, 0x641c, 0x641c, 
  255. 0x641c, 0x641c, 0x641c, 0x641c, 
  256. 0x601c, 0x601c, 0x601c, 0x601c, 
  257. 0x601c, 0x601c, 0x601c, 0x601c, 
  258. 0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  259. 0x5c1c, 0x5c1c, 0x5c1c, 0x5c1c, 
  260. 0x581c, 0x581c, 0x581c, 0x581c, 
  261. 0x581c, 0x581c, 0x581c, 0x581c, 
  262. };
  263.  
  264. unsigned short int dct_coeff_tbl_1[16] = 
  265. {
  266. 0x00bb, 0x202b, 0x103b, 0x00ab, 
  267. 0x084b, 0x1c2b, 0x541b, 0x501b, 
  268. 0x009b, 0x4c1b, 0x481b, 0x045b, 
  269. 0x0c3b, 0x008b, 0x182b, 0x441b, 
  270. };
  271.  
  272. unsigned short int dct_coeff_tbl_2[4] =
  273. {
  274. 0x4019, 0x1429, 0x0079, 0x0839, 
  275. };
  276.  
  277. unsigned short int dct_coeff_tbl_3[4] = 
  278. {
  279. 0x0449, 0x3c19, 0x3819, 0x1029, 
  280. };
  281.  
  282. unsigned short int dct_coeff_next[256] = 
  283. {
  284. 0xffff, 0xffff, 0xffff, 0xffff, 
  285. 0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  286. 0x0826, 0x0826, 0x2416, 0x2416, 
  287. 0x0046, 0x0046, 0x2016, 0x2016, 
  288. 0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  289. 0x1815, 0x1815, 0x1815, 0x1815, 
  290. 0x0425, 0x0425, 0x0425, 0x0425, 
  291. 0x1415, 0x1415, 0x1415, 0x1415, 
  292. 0x3417, 0x0067, 0x3017, 0x2c17, 
  293. 0x0c27, 0x0437, 0x0057, 0x2817, 
  294. 0x0034, 0x0034, 0x0034, 0x0034, 
  295. 0x0034, 0x0034, 0x0034, 0x0034, 
  296. 0x1014, 0x1014, 0x1014, 0x1014, 
  297. 0x1014, 0x1014, 0x1014, 0x1014, 
  298. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  299. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  300. 0x0023, 0x0023, 0x0023, 0x0023, 
  301. 0x0023, 0x0023, 0x0023, 0x0023, 
  302. 0x0023, 0x0023, 0x0023, 0x0023, 
  303. 0x0023, 0x0023, 0x0023, 0x0023, 
  304. 0x0813, 0x0813, 0x0813, 0x0813, 
  305. 0x0813, 0x0813, 0x0813, 0x0813, 
  306. 0x0813, 0x0813, 0x0813, 0x0813, 
  307. 0x0813, 0x0813, 0x0813, 0x0813, 
  308. 0x0412, 0x0412, 0x0412, 0x0412, 
  309. 0x0412, 0x0412, 0x0412, 0x0412, 
  310. 0x0412, 0x0412, 0x0412, 0x0412, 
  311. 0x0412, 0x0412, 0x0412, 0x0412, 
  312. 0x0412, 0x0412, 0x0412, 0x0412, 
  313. 0x0412, 0x0412, 0x0412, 0x0412, 
  314. 0x0412, 0x0412, 0x0412, 0x0412, 
  315. 0x0412, 0x0412, 0x0412, 0x0412, 
  316. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  317. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  318. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  319. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  320. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  321. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  322. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  323. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  324. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  325. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  326. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  327. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  328. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  329. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  330. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  331. 0xfbe1, 0xfbe1, 0xfbe1, 0xfbe1, 
  332. 0x0011, 0x0011, 0x0011, 0x0011, 
  333. 0x0011, 0x0011, 0x0011, 0x0011, 
  334. 0x0011, 0x0011, 0x0011, 0x0011, 
  335. 0x0011, 0x0011, 0x0011, 0x0011, 
  336. 0x0011, 0x0011, 0x0011, 0x0011, 
  337. 0x0011, 0x0011, 0x0011, 0x0011, 
  338. 0x0011, 0x0011, 0x0011, 0x0011, 
  339. 0x0011, 0x0011, 0x0011, 0x0011, 
  340. 0x0011, 0x0011, 0x0011, 0x0011, 
  341. 0x0011, 0x0011, 0x0011, 0x0011, 
  342. 0x0011, 0x0011, 0x0011, 0x0011, 
  343. 0x0011, 0x0011, 0x0011, 0x0011, 
  344. 0x0011, 0x0011, 0x0011, 0x0011, 
  345. 0x0011, 0x0011, 0x0011, 0x0011, 
  346. 0x0011, 0x0011, 0x0011, 0x0011, 
  347. 0x0011, 0x0011, 0x0011, 0x0011, 
  348. };
  349.  
  350. unsigned short int dct_coeff_first[256] = 
  351. {
  352. 0xffff, 0xffff, 0xffff, 0xffff, 
  353. 0xf7d5, 0xf7d5, 0xf7d5, 0xf7d5, 
  354. 0x0826, 0x0826, 0x2416, 0x2416, 
  355. 0x0046, 0x0046, 0x2016, 0x2016, 
  356. 0x1c15, 0x1c15, 0x1c15, 0x1c15, 
  357. 0x1815, 0x1815, 0x1815, 0x1815, 
  358. 0x0425, 0x0425, 0x0425, 0x0425, 
  359. 0x1415, 0x1415, 0x1415, 0x1415, 
  360. 0x3417, 0x0067, 0x3017, 0x2c17, 
  361. 0x0c27, 0x0437, 0x0057, 0x2817, 
  362. 0x0034, 0x0034, 0x0034, 0x0034, 
  363. 0x0034, 0x0034, 0x0034, 0x0034, 
  364. 0x1014, 0x1014, 0x1014, 0x1014, 
  365. 0x1014, 0x1014, 0x1014, 0x1014, 
  366. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  367. 0x0c14, 0x0c14, 0x0c14, 0x0c14, 
  368. 0x0023, 0x0023, 0x0023, 0x0023, 
  369. 0x0023, 0x0023, 0x0023, 0x0023, 
  370. 0x0023, 0x0023, 0x0023, 0x0023, 
  371. 0x0023, 0x0023, 0x0023, 0x0023, 
  372. 0x0813, 0x0813, 0x0813, 0x0813, 
  373. 0x0813, 0x0813, 0x0813, 0x0813, 
  374. 0x0813, 0x0813, 0x0813, 0x0813, 
  375. 0x0813, 0x0813, 0x0813, 0x0813, 
  376. 0x0412, 0x0412, 0x0412, 0x0412, 
  377. 0x0412, 0x0412, 0x0412, 0x0412, 
  378. 0x0412, 0x0412, 0x0412, 0x0412, 
  379. 0x0412, 0x0412, 0x0412, 0x0412, 
  380. 0x0412, 0x0412, 0x0412, 0x0412, 
  381. 0x0412, 0x0412, 0x0412, 0x0412, 
  382. 0x0412, 0x0412, 0x0412, 0x0412, 
  383. 0x0412, 0x0412, 0x0412, 0x0412, 
  384. 0x0010, 0x0010, 0x0010, 0x0010, 
  385. 0x0010, 0x0010, 0x0010, 0x0010, 
  386. 0x0010, 0x0010, 0x0010, 0x0010, 
  387. 0x0010, 0x0010, 0x0010, 0x0010, 
  388. 0x0010, 0x0010, 0x0010, 0x0010, 
  389. 0x0010, 0x0010, 0x0010, 0x0010, 
  390. 0x0010, 0x0010, 0x0010, 0x0010, 
  391. 0x0010, 0x0010, 0x0010, 0x0010, 
  392. 0x0010, 0x0010, 0x0010, 0x0010, 
  393. 0x0010, 0x0010, 0x0010, 0x0010, 
  394. 0x0010, 0x0010, 0x0010, 0x0010, 
  395. 0x0010, 0x0010, 0x0010, 0x0010, 
  396. 0x0010, 0x0010, 0x0010, 0x0010, 
  397. 0x0010, 0x0010, 0x0010, 0x0010, 
  398. 0x0010, 0x0010, 0x0010, 0x0010, 
  399. 0x0010, 0x0010, 0x0010, 0x0010, 
  400. 0x0010, 0x0010, 0x0010, 0x0010, 
  401. 0x0010, 0x0010, 0x0010, 0x0010, 
  402. 0x0010, 0x0010, 0x0010, 0x0010, 
  403. 0x0010, 0x0010, 0x0010, 0x0010, 
  404. 0x0010, 0x0010, 0x0010, 0x0010, 
  405. 0x0010, 0x0010, 0x0010, 0x0010, 
  406. 0x0010, 0x0010, 0x0010, 0x0010, 
  407. 0x0010, 0x0010, 0x0010, 0x0010, 
  408. 0x0010, 0x0010, 0x0010, 0x0010, 
  409. 0x0010, 0x0010, 0x0010, 0x0010, 
  410. 0x0010, 0x0010, 0x0010, 0x0010, 
  411. 0x0010, 0x0010, 0x0010, 0x0010, 
  412. 0x0010, 0x0010, 0x0010, 0x0010, 
  413. 0x0010, 0x0010, 0x0010, 0x0010, 
  414. 0x0010, 0x0010, 0x0010, 0x0010, 
  415. 0x0010, 0x0010, 0x0010, 0x0010, 
  416. };
  417.  
  418. /* Macro for filling up the decoding table for mb_addr_inc */
  419. #define ASSIGN1(start, end, step, val, num) \
  420.   for (i = start; i < end; i+= step) { \
  421.     for (j = 0; j < step; j++) { \
  422.       mb_addr_inc[i+j].value = val; \
  423.       mb_addr_inc[i+j].num_bits = num; \
  424.     } \
  425.     val--; \
  426.     }
  427.  
  428.  
  429.  
  430. /*
  431.  *--------------------------------------------------------------
  432.  *
  433.  * init_mb_addr_inc --
  434.  *
  435.  *    Initialize the VLC decoding table for macro_block_address_increment
  436.  *
  437.  * Results:
  438.  *    The decoding table for macro_block_address_increment will
  439.  *      be filled; illegal values will be filled as ERROR.
  440.  *
  441.  * Side effects:
  442.  *    The global array mb_addr_inc will be filled.
  443.  *
  444.  *--------------------------------------------------------------
  445.  */
  446. static void
  447. init_mb_addr_inc()
  448. {
  449.   int i, j, val;
  450.  
  451.   for (i = 0; i < 8; i++) {
  452.     mb_addr_inc[i].value = ERROR;
  453.     mb_addr_inc[i].num_bits = 0;
  454.   }
  455.  
  456.   mb_addr_inc[8].value = MACRO_BLOCK_ESCAPE;
  457.   mb_addr_inc[8].num_bits = 11;
  458.  
  459.   for (i = 9; i < 15; i++) {
  460.     mb_addr_inc[i].value = ERROR;
  461.     mb_addr_inc[i].num_bits = 0;
  462.   }
  463.  
  464.   mb_addr_inc[15].value = MACRO_BLOCK_STUFFING;
  465.   mb_addr_inc[15].num_bits = 11;
  466.  
  467.   for (i = 16; i < 24; i++) {
  468.     mb_addr_inc[i].value = ERROR;
  469.     mb_addr_inc[i].num_bits = 0;
  470.   }
  471.  
  472.   val = 33;
  473.  
  474.   ASSIGN1(24, 36, 1, val, 11);
  475.   ASSIGN1(36, 48, 2, val, 10);
  476.   ASSIGN1(48, 96, 8, val, 8);
  477.   ASSIGN1(96, 128, 16, val, 7);
  478.   ASSIGN1(128, 256, 64, val, 5);
  479.   ASSIGN1(256, 512, 128, val, 4);
  480.   ASSIGN1(512, 1024, 256, val, 3);
  481.   ASSIGN1(1024, 2048, 1024, val, 1);
  482. }
  483.  
  484.  
  485. /* Macro for filling up the decoding table for mb_type */
  486. #define ASSIGN2(start, end, quant, motion_forward, motion_backward, pattern, intra, num, mb_type) \
  487.   for (i = start; i < end; i ++) { \
  488.     mb_type[i].mb_quant = quant; \
  489.     mb_type[i].mb_motion_forward = motion_forward; \
  490.     mb_type[i].mb_motion_backward = motion_backward; \
  491.     mb_type[i].mb_pattern = pattern; \
  492.     mb_type[i].mb_intra = intra; \
  493.     mb_type[i].num_bits = num; \
  494.   }
  495.      
  496.  
  497.  
  498. /*
  499.  *--------------------------------------------------------------
  500.  *
  501.  * init_mb_type_P --
  502.  *
  503.  *    Initialize the VLC decoding table for macro_block_type in
  504.  *      predictive-coded pictures.
  505.  *
  506.  * Results:
  507.  *    The decoding table for macro_block_type in predictive-coded
  508.  *      pictures will be filled; illegal values will be filled as ERROR.
  509.  *
  510.  * Side effects:
  511.  *    The global array mb_type_P will be filled.
  512.  *
  513.  *--------------------------------------------------------------
  514.  */
  515. static void
  516. init_mb_type_P()
  517. {
  518.   int i;
  519.  
  520.   mb_type_P[0].mb_quant = mb_type_P[0].mb_motion_forward 
  521.     = mb_type_P[0].mb_motion_backward = mb_type_P[0].mb_pattern 
  522.       = mb_type_P[0].mb_intra = ERROR;
  523.   mb_type_P[0].num_bits = 0;
  524.  
  525.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_P)
  526.   ASSIGN2(2, 4, 1, 0, 0, 1, 0, 5, mb_type_P)
  527.   ASSIGN2(4, 6, 1, 1, 0, 1, 0, 5, mb_type_P);
  528.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_P);
  529.   ASSIGN2(8, 16, 0, 1, 0, 0, 0, 3, mb_type_P);
  530.   ASSIGN2(16, 32, 0, 0, 0, 1, 0, 2, mb_type_P);
  531.   ASSIGN2(32, 64, 0, 1, 0, 1, 0, 1, mb_type_P);
  532. }
  533.  
  534.  
  535.  
  536.  
  537. /*
  538.  *--------------------------------------------------------------
  539.  *
  540.  * init_mb_type_B --
  541.  *
  542.  *    Initialize the VLC decoding table for macro_block_type in
  543.  *      bidirectionally-coded pictures.
  544.  *
  545.  * Results:
  546.  *    The decoding table for macro_block_type in bidirectionally-coded
  547.  *      pictures will be filled; illegal values will be filled as ERROR.
  548.  *
  549.  * Side effects:
  550.  *    The global array mb_type_B will be filled.
  551.  *
  552.  *--------------------------------------------------------------
  553.  */
  554. static void
  555. init_mb_type_B()
  556. {
  557.   int i;
  558.  
  559.   mb_type_B[0].mb_quant = mb_type_B[0].mb_motion_forward 
  560.     = mb_type_B[0].mb_motion_backward = mb_type_B[0].mb_pattern 
  561.       = mb_type_B[0].mb_intra = ERROR;
  562.   mb_type_B[0].num_bits = 0;
  563.  
  564.   ASSIGN2(1, 2, 1, 0, 0, 0, 1, 6, mb_type_B);
  565.   ASSIGN2(2, 3, 1, 0, 1, 1, 0, 6, mb_type_B);
  566.   ASSIGN2(3, 4, 1, 1, 0, 1, 0, 6, mb_type_B);
  567.   ASSIGN2(4, 6, 1, 1, 1, 1, 0, 5, mb_type_B);
  568.   ASSIGN2(6, 8, 0, 0, 0, 0, 1, 5, mb_type_B);
  569.   ASSIGN2(8, 12, 0, 1, 0, 0, 0, 4, mb_type_B);
  570.   ASSIGN2(12, 16, 0, 1, 0, 1, 0, 4, mb_type_B);
  571.   ASSIGN2(16, 24, 0, 0, 1, 0, 0, 3, mb_type_B);
  572.   ASSIGN2(24, 32, 0, 0, 1, 1, 0, 3, mb_type_B);
  573.   ASSIGN2(32, 48, 0, 1, 1, 0, 0, 2, mb_type_B);
  574.   ASSIGN2(48, 64, 0, 1, 1, 1, 0, 2, mb_type_B);
  575. }
  576.  
  577.  
  578. /* Macro for filling up the decoding tables for motion_vectors */
  579. #define ASSIGN3(start, end, step, val, num) \
  580.   for (i = start; i < end; i+= step) { \
  581.     for (j = 0; j < step / 2; j++) { \
  582.       motion_vectors[i+j].code = val; \
  583.       motion_vectors[i+j].num_bits = num; \
  584.     } \
  585.     for (j = step / 2; j < step; j++) { \
  586.       motion_vectors[i+j].code = -val; \
  587.       motion_vectors[i+j].num_bits = num; \
  588.     } \
  589.     val--; \
  590.   }
  591.  
  592.  
  593.  
  594. /*
  595.  *--------------------------------------------------------------
  596.  *
  597.  * init_motion_vectors --
  598.  *
  599.  *    Initialize the VLC decoding table for the various motion
  600.  *      vectors, including motion_horizontal_forward_code, 
  601.  *      motion_vertical_forward_code, motion_horizontal_backward_code,
  602.  *      and motion_vertical_backward_code.
  603.  *
  604.  * Results:
  605.  *    The decoding table for the motion vectors will be filled;
  606.  *      illegal values will be filled as ERROR.
  607.  *
  608.  * Side effects:
  609.  *    The global array motion_vector will be filled.
  610.  *
  611.  *--------------------------------------------------------------
  612.  */
  613. static void
  614. init_motion_vectors()
  615. {
  616.   int i, j, val = 16;
  617.  
  618.   for (i = 0; i < 24; i++) {
  619.     motion_vectors[i].code = ERROR;
  620.     motion_vectors[i].num_bits = 0;
  621.   }
  622.  
  623.   ASSIGN3(24, 36, 2, val, 11);
  624.   ASSIGN3(36, 48, 4, val, 10);
  625.   ASSIGN3(48, 96, 16, val, 8);
  626.   ASSIGN3(96, 128, 32, val, 7);
  627.   ASSIGN3(128, 256, 128, val, 5);
  628.   ASSIGN3(256, 512, 256, val, 4);
  629.   ASSIGN3(512, 1024, 512, val, 3);
  630.   ASSIGN3(1024, 2048, 1024, val, 1);
  631. }
  632.  
  633.  
  634.  
  635.  
  636. /*
  637.  *--------------------------------------------------------------
  638.  *
  639.  * init_tables --
  640.  *
  641.  *    Initialize all the tables for VLC decoding; this must be
  642.  *      called when the system is set up before any decoding can
  643.  *      take place.
  644.  *
  645.  * Results:
  646.  *    All the decoding tables will be filled accordingly.
  647.  *
  648.  * Side effects:
  649.  *    The corresponding global array for each decoding table 
  650.  *      will be filled.
  651.  *
  652.  *--------------------------------------------------------------
  653.  */    
  654. void
  655. init_tables()
  656. {
  657.   extern void init_pre_idct();
  658.  
  659.   init_mb_addr_inc();
  660.   init_mb_type_P();
  661.   init_mb_type_B();
  662.   init_motion_vectors();
  663.   init_pre_idct();
  664.  
  665.   {
  666.     init_stats();
  667.   }
  668. }
  669.  
  670. /*
  671.  *--------------------------------------------------------------
  672.  *
  673.  * DecodeDCTDCSizeLum --
  674.  *
  675.  *    Huffman Decoder for dct_dc_size_luminance; location where
  676.  *      the result of decoding will be placed is passed as argument.
  677.  *      The decoded values are obtained by doing a table lookup on
  678.  *      dct_dc_size_luminance.
  679.  *
  680.  * Results:
  681.  *    The decoded value for dct_dc_size_luminance or ERROR for 
  682.  *      unbound values will be placed in the location specified.
  683.  *
  684.  * Side effects:
  685.  *    Bit stream is irreversibly parsed.
  686.  *
  687.  *--------------------------------------------------------------
  688.  */        
  689. void
  690. decodeDCTDCSizeLum(value)
  691. unsigned int *value;
  692. {
  693.   unsigned int index;
  694.  
  695.   show_bits7(index);
  696.   
  697.   *value = dct_dc_size_luminance[index].value;
  698.  
  699.   flush_bits(dct_dc_size_luminance[index].num_bits);
  700. }
  701.  
  702.  
  703.  
  704.  
  705. /*
  706.  *--------------------------------------------------------------
  707.  *
  708.  * DecodeDCTDCSizeChrom --
  709.  *
  710.  *    Huffman Decoder for dct_dc_size_chrominance; location where
  711.  *      the result of decoding will be placed is passed as argument.
  712.  *      The decoded values are obtained by doing a table lookup on
  713.  *      dct_dc_size_chrominance.
  714.  *
  715.  * Results:
  716.  *    The decoded value for dct_dc_size_chrominance or ERROR for
  717.  *      unbound values will be placed in the location specified.
  718.  *
  719.  * Side effects:
  720.  *    Bit stream is irreversibly parsed.
  721.  *
  722.  *--------------------------------------------------------------
  723.  */    
  724. void    
  725. decodeDCTDCSizeChrom(value)
  726. unsigned int *value;
  727. {
  728.   unsigned int index;
  729.  
  730.   show_bits8(index);
  731.   
  732.   *value = dct_dc_size_chrominance[index].value;
  733.  
  734.   flush_bits(dct_dc_size_chrominance[index].num_bits);
  735. }
  736.  
  737.  
  738.  
  739. /*
  740.  *--------------------------------------------------------------
  741.  *
  742.  * decodeDCTCoeff --
  743.  *
  744.  *    Huffman Decoder for dct_coeff_first and dct_coeff_next;
  745.  *      locations where the results of decoding: run and level, are to
  746.  *      be placed and also the type of DCT coefficients, either
  747.  *      dct_coeff_first or dct_coeff_next, are being passed as argument.
  748.  *      
  749.  *      The decoder first examines the next 8 bits in the input stream,
  750.  *      and perform according to the following cases:
  751.  *      
  752.  *      '0000 0000' - examine 8 more bits (i.e. 16 bits total) and
  753.  *                    perform a table lookup on dct_coeff_tbl_0.
  754.  *                    One more bit is then examined to determine the sign
  755.  *                    of level.
  756.  *
  757.  *      '0000 0001' - examine 4 more bits (i.e. 12 bits total) and 
  758.  *                    perform a table lookup on dct_coeff_tbl_1.
  759.  *                    One more bit is then examined to determine the sign
  760.  *                    of level.
  761.  *      
  762.  *      '0000 0010' - examine 2 more bits (i.e. 10 bits total) and
  763.  *                    perform a table lookup on dct_coeff_tbl_2.
  764.  *                    One more bit is then examined to determine the sign
  765.  *                    of level.
  766.  *
  767.  *      '0000 0011' - examine 2 more bits (i.e. 10 bits total) and 
  768.  *                    perform a table lookup on dct_coeff_tbl_3.
  769.  *                    One more bit is then examined to determine the sign
  770.  *                    of level.
  771.  *
  772.  *      otherwise   - perform a table lookup on dct_coeff_tbl. If the
  773.  *                    value of run is not ESCAPE, extract one more bit
  774.  *                    to determine the sign of level; otherwise 6 more
  775.  *                    bits will be extracted to obtain the actual value 
  776.  *                    of run , and then 8 or 16 bits to get the value of level.
  777.  *                    
  778.  *      
  779.  *
  780.  * Results:
  781.  *    The decoded values of run and level or ERROR for unbound values
  782.  *      are placed in the locations specified.
  783.  *
  784.  * Side effects:
  785.  *    Bit stream is irreversibly parsed.
  786.  *
  787.  *--------------------------------------------------------------
  788.  */
  789. static void
  790. decodeDCTCoeff(dct_coeff_tbl, run, level)
  791. unsigned short int *dct_coeff_tbl;                                       
  792. unsigned int *run;
  793. int *level;
  794. {
  795.   unsigned int temp, index, num_bits;
  796.   unsigned int value, next32bits, flushed;
  797.  
  798.   /*
  799.    * Grab the next 32 bits and use it to improve performance of
  800.    * getting the bits to parse. Thus, calls are translated as:
  801.    *
  802.    *    show_bitsX  <-->   next32bits >> (32-X)
  803.    *    get_bitsX   <-->   val = next32bits >> (32-flushed-X);
  804.    *               flushed += X;
  805.    *               next32bits &= bitMask[flushed];
  806.    *    flush_bitsX <-->   flushed += X;
  807.    *               next32bits &= bitMask[flushed];
  808.    *
  809.    */
  810.   show_bits32(next32bits);
  811.   flushed = 0;
  812.  
  813.   /* show_bits8(index); */
  814.   index = next32bits >> 24;
  815.  
  816.   if (index > 3) {
  817.     value = dct_coeff_tbl[index];
  818.     *run = (value & RUN_MASK) >> RUN_SHIFT;
  819.     if (*run == END_OF_BLOCK) {
  820.       *level = END_OF_BLOCK;
  821.     }
  822.     else {
  823.       /* num_bits = (value & NUM_MASK) + 1; */
  824.       /* flush_bits(num_bits); */
  825.       flushed = (value & NUM_MASK) + 1;
  826.       next32bits &= bitMask[flushed];
  827.       if (*run != ESCAPE) {
  828.          *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
  829.      /* get_bits1(value); */
  830.      /* if (value) *level = -*level; */
  831.      if (next32bits >> (31-flushed)) *level = -*level;
  832.      flushed++;
  833.      /* next32bits &= bitMask[flushed];  last op before update */
  834.        }
  835.        else {    /* *run == ESCAPE */
  836.          /* get_bits14(temp); */
  837.      temp = next32bits >> (18-flushed);
  838.      flushed += 14;
  839.      next32bits &= bitMask[flushed];
  840.      *run = temp >> 8;
  841.      temp &= 0xff;
  842.      if (temp == 0) {
  843.             /* get_bits8(*level); */
  844.         *level = next32bits >> (24-flushed);
  845.         flushed += 8;
  846.         /* next32bits &= bitMask[flushed];  last op before update */
  847.          assert(*level >= 128);
  848.      } else if (temp != 128) {
  849.         /* Grab sign bit */
  850.         *level = ((int) (temp << 24)) >> 24;
  851.      } else {
  852.             /* get_bits8(*level); */
  853.         *level = next32bits >> (24-flushed);
  854.         flushed += 8;
  855.         /* next32bits &= bitMask[flushed];  last op before update */
  856.         *level = *level - 256;
  857.         assert(*level <= -128 && *level >= -255);
  858.      }
  859.        }
  860.        /* Update bitstream... */
  861.        flush_bits(flushed);
  862.     }
  863.   }
  864.   else {
  865.     if (index == 2) { 
  866.       /* show_bits10(index); */
  867.       index = next32bits >> 22;
  868.       value = dct_coeff_tbl_2[index & 3];
  869.     }
  870.     else if (index == 3) { 
  871.       /* show_bits10(index); */
  872.       index = next32bits >> 22;
  873.       value = dct_coeff_tbl_3[index & 3];
  874.     }
  875.     else if (index) {    /* index == 1 */
  876.       /* show_bits12(index); */
  877.       index = next32bits >> 20;
  878.       value = dct_coeff_tbl_1[index & 15];
  879.     }
  880.     else {   /* index == 0 */
  881.       /* show_bits16(index); */
  882.       index = next32bits >> 16;
  883.       value = dct_coeff_tbl_0[index & 255];
  884.     }
  885.     *run = (value & RUN_MASK) >> RUN_SHIFT;
  886.     *level = (value & LEVEL_MASK) >> LEVEL_SHIFT;
  887.  
  888.     /*
  889.      * Fold these operations together to make it fast...
  890.      */
  891.     /* num_bits = (value & NUM_MASK) + 1; */
  892.     /* flush_bits(num_bits); */
  893.     /* get_bits1(value); */
  894.     /* if (value) *level = -*level; */
  895.  
  896.     flushed = (value & NUM_MASK) + 2;
  897.     if ((next32bits >> (32-flushed)) & 0x1) *level = -*level;
  898.  
  899.     /* Update bitstream ... */
  900.     flush_bits(flushed);
  901.   }
  902. }
  903.  
  904.  
  905. /*
  906.  *--------------------------------------------------------------
  907.  *
  908.  * decodeDCTCoeffFirst --
  909.  *
  910.  *    Huffman Decoder for dct_coeff_first. Locations for the
  911.  *      decoded results: run and level, are being passed as
  912.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  913.  *      with the table dct_coeff_first.
  914.  *
  915.  * Results:
  916.  *    The decoded values of run and level for dct_coeff_first or
  917.  *      ERROR for unbound values are placed in the locations given.
  918.  *
  919.  * Side effects:
  920.  *    Bit stream is irreversibly parsed.
  921.  *
  922.  *--------------------------------------------------------------
  923.  */        
  924. void
  925. decodeDCTCoeffFirst(run, level)
  926. unsigned int *run;
  927. int *level;
  928. {
  929.   decodeDCTCoeff(dct_coeff_first, run, level);
  930. }
  931.  
  932.  
  933.  
  934.  
  935. /*
  936.  *--------------------------------------------------------------
  937.  *
  938.  * decodeDCTCoeffNext --
  939.  *
  940.  *    Huffman Decoder for dct_coeff_first. Locations for the
  941.  *      decoded results: run and level, are being passed as
  942.  *      arguments. Actual work is being done by calling DecodeDCTCoeff,
  943.  *      with the table dct_coeff_next.
  944.  *
  945.  * Results:
  946.  *    The decoded values of run and level for dct_coeff_next or
  947.  *      ERROR for unbound values are placed in the locations given.
  948.  *
  949.  * Side effects:
  950.  *    Bit stream is irreversibly parsed.
  951.  *
  952.  *--------------------------------------------------------------
  953.  */ 
  954. void       
  955. decodeDCTCoeffNext(run, level)
  956. unsigned int *run;
  957. int *level;
  958. {
  959.   decodeDCTCoeff(dct_coeff_next, run, level);
  960. }
  961.